using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class AnimWheelLeg : MonoBehaviour
{
    public GameObject Top;
    private GameObject Bottom;
    private GameObject Front;
    private GameObject Back;
    public WheelActuator wheel;
    public GameObject leg;
    public GameObject point;
    public float leg_length = 1;
    public float k = 1;
    public GameObject[] legs;
    // Start is called before the first frame update
    void Start()
    {
        legs=new GameObject[4];
        legs[0]=Instantiate(leg, this.transform);
        legs[1] = Instantiate(leg, this.transform);
        legs[2] = Instantiate(leg, this.transform);
        legs[3] = Instantiate(leg, this.transform);
        Bottom=Instantiate(point, this.transform);
        Front =Instantiate(point, this.transform);
        Back=Instantiate(point, this.transform);
        Bottom.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        Front.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        Back.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        foreach (GameObject leg in legs)
        {
            leg.transform.localScale = new Vector3(0.1f, 0.1f, leg_length);
        }
    }

    // Update is called once per frame
    void Update()
    {
        var bottom_pos = wheel.WheelAxis.transform.position - wheel.WheelAxis.transform.forward * 0.2f*k;
        var t_b_v=Top.transform.position - bottom_pos;
        Vector3 rotationAxis = Top.transform.right;
        Quaternion rotation = Quaternion.AngleAxis(90, rotationAxis);
        Vector3 c_dir = (rotation*t_b_v).normalized;
        Vector3 center = bottom_pos +  t_b_v / 2;
        //Front.transform.position = center;
        float c_len = Mathf.Sqrt(leg_length * leg_length - t_b_v.magnitude * t_b_v.magnitude / 4);
        Front.transform.position = center + c_dir * c_len;
        Back.transform.position = center - c_dir * c_len;
        //Bottom.transform.position = new Vector3(Bottom.transform.position.x, wheel.WheelAxis.transform.position.y, Bottom.transform.position.z);
        //float height=Top.transform.position.y-Bottom.transform.position.y;
        Bottom.transform.position = bottom_pos;
        legs[0].transform.position = Top.transform.position + (Front.transform.position - Top.transform.position) / 2;
        legs[0].transform.rotation= Quaternion.LookRotation(Front.transform.position - Top.transform.position);
        SetLegTransform(legs[0],Top.transform.position,Front.transform.position);
        SetLegTransform(legs[1], Top.transform.position, Back.transform.position);
        SetLegTransform(legs[2], Bottom.transform.position, Front.transform.position);
        SetLegTransform(legs[3], Bottom.transform.position, Back.transform.position);
    }
    private void SetLegTransform(GameObject leg,Vector3 from, Vector3 dst)
    {
        Vector3 dir = dst - from;
        leg.transform.position = from + dir / 2;
        leg.transform.rotation = Quaternion.LookRotation(dir);
    }
}
